$(function(){
    var loading = false;
    var get = false;
    var s = 10*1000;
    //上拉加载更多
    $('#news').swipeUp(function(){
        if(!loading){//不是在load
            loading = true;
            getHistory();
            loading = false;
        };
    });
    var getHistory = function(){
        loading = true;
        lastpage = $("#pageid").text();
        $.get("/ajax-getnews-page-"+lastpage+".html", function(result){
            if( result.ret ){
                $("#pageid").text( lastpage*1 + 1 );
                $("#news").append(result.data);
            }else{
                if( $(".content-end").length <= 0 ){
                    $("#news").after('<p class="content-end"><i></i>我是有底线的<i></i></p>');
                }
            }
        });
    };
    var getNews = function(){
        if(!get){//不是get
            get = true;
            var newesttime = $("#newesttime").text();
            $.get("/ajax-getmore-"+newesttime +".html", function(result){
                if( result.count*1 ){
                    $("#news-num").html(result.count);
                    $(".news-tip").addClass('show');
                    $(".fixed-title").show();
                    $(".news").addClass('news-fixed');
                };
                get = false;
            });
        };
    };
    $('.news-tip').click(function(){
        if($(this).hasClass('show')){
            window.location.reload();
        }
    });
    //定时查询
    var time = setInterval(getNews,s);
    //滚动漂浮
    $('.fixed-title').html($($('.news-item dt')[0]).html());
    var dt = $('.news-item dt');
    var top = 44;//$(".fixed-title").offset().top;

    window.onscroll = function(){
        for(var i=0;i<dt.length;i++){
            var index = i;
            var dtTop = $(dt[index]).offset().top - $('body').scrollTop();
            //console.log(top,'--------------',dtTop);
            if(dtTop < top){
                $(dt[index]).addClass('dtHide');
            }else{
                $(dt[index]).removeClass('dtHide');
            };
        };
        var hideS = $('.dtHide');
        if(hideS.length > 0){
            var html = $(hideS[hideS.length-1]).html();
            $(".fixed-title").html(html).show();
        };
    };
});
